
public class MethodDecl {
	String st;
	Type type;
	String id;
	String lp;
	FormalParams fp;
	String rp;
	Block b;
	public MethodDecl(String st, Type type, String id, String lp, FormalParams fp, String rp, Block b){
		this.st = st;
		this.type = type;
		this.id = id ;
		this.lp = lp;
		this.fp = fp;
		this.rp = rp;
		this.b = b;
	}
	public String toString(){
		String ret = "MethodDecl "+ id+ "\n";

		String s = "";

		if (fp != null) {
			s +=  fp.toString();
		}
		
		
		if (b != null) {
			s += b.toString();
		}

		for(String st: s.split("\n")){
			if(st!= "")
				ret += "| " + st + "\n";
		}

		return ret;
	}
	public void check() throws SemanticException {
		SymbolTable table = SymbolTable.getInstance();
		if (table.contains(id)) {
			throw new SemanticException("The identifier " + id + " has been used before");
		}
		table.add(new Entry(id, type, true));
		fp.check();
		b.check();
		// //System.out.println("if " + table.getNumOfIfs() + " return " + table.getNumOfReturns() + " " + table.getInIf() + " " + table.getUnCondIf());
		// check if there is an uncodnitional return stmt
		if (!table.getUnCondIf() && table.getNumOfIfs() != table.getNumOfReturns()) {	
			throw new SemanticException("Missing return statement");
		} 
		table.resetIfCounters();
		table.toggleUnCondIf();
	}
}
